//
//  NDSpeechBubbleShape.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/28.
//

import SwiftUI

struct NDSpeechBubbleShape: Shape {
    var cornerRadius: CGFloat
    var arrowHeight: CGFloat  // 箭头总高度，同时控制曲线起伏
    var arrowWidth: CGFloat = 50
    var arrowPosition: CGFloat = 0.5 // 0.0 最左边，1.0 最右边，0.5 中间
    
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        // 根据 arrowPosition 计算箭头的水平位置
        let arrowCenterX = rect.minX + (rect.width * arrowPosition)
        let startX = arrowCenterX - arrowWidth / 2
        let endX = arrowCenterX + arrowWidth / 2
        
        let curveHeight = arrowHeight
        
        // 从左上角开始
        path.move(to: CGPoint(x: rect.minX + cornerRadius, y: rect.minY + arrowHeight))
        
        // 左侧到三角起点
        path.addLine(to: CGPoint(x: startX, y: rect.minY + arrowHeight))
        
        // 使用三次贝塞尔曲线创建正弦波效果
        // 左侧曲线到中点
        path.addCurve(to: CGPoint(x: arrowCenterX, y: rect.minY + arrowHeight - curveHeight),
                     control1: CGPoint(x: startX + arrowWidth * 0.25, y: rect.minY + arrowHeight),
                     control2: CGPoint(x: arrowCenterX - arrowWidth * 0.1, y: rect.minY + arrowHeight - curveHeight))
        
        // 中点到右侧
        path.addCurve(to: CGPoint(x: endX, y: rect.minY + arrowHeight),
                     control1: CGPoint(x: arrowCenterX + arrowWidth * 0.1, y: rect.minY + arrowHeight - curveHeight),
                     control2: CGPoint(x: endX - arrowWidth * 0.25, y: rect.minY + arrowHeight))
        
        // 右侧到右上角
        path.addLine(to: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY + arrowHeight))
        
        // 右上圆角
        path.addArc(center: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY + arrowHeight + cornerRadius),
                   radius: cornerRadius,
                   startAngle: Angle(degrees: -90),
                   endAngle: Angle(degrees: 0),
                   clockwise: false)
        
        // 右下角
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - cornerRadius))
        path.addArc(center: CGPoint(x: rect.maxX - cornerRadius, y: rect.maxY - cornerRadius),
                   radius: cornerRadius,
                   startAngle: Angle(degrees: 0),
                   endAngle: Angle(degrees: 90),
                   clockwise: false)
        
        // 左下角
        path.addLine(to: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY))
        path.addArc(center: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY - cornerRadius),
                   radius: cornerRadius,
                   startAngle: Angle(degrees: 90),
                   endAngle: Angle(degrees: 180),
                   clockwise: false)
        
        // 回到左上角
        path.addLine(to: CGPoint(x: rect.minX, y: rect.minY + arrowHeight + cornerRadius))
        path.addArc(center: CGPoint(x: rect.minX + cornerRadius, y: rect.minY + arrowHeight + cornerRadius),
                   radius: cornerRadius,
                   startAngle: Angle(degrees: 180),
                   endAngle: Angle(degrees: 270),
                   clockwise: false)
        
        path.closeSubpath()
        return path
    }
}
